<?php
namespace shareTrait\Api;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 *
 * Description of DataProcessor
 *
 * @author hardy
 */
trait DataProcessor
{
    use Response;
    /**
     * 更新数据并返回数据
     * @param array $data
     * @param $model
     * @param string $fun
     */
    public function updateResponse($data=[],$model=CONTROLLER_NAME,$fun='getOneData'){
        if(is_string($model)){
            $model = D($model);
        }
        if(empty($data)){
            $data = I('put.');
        }
        if($model->createSave($data) === false){
            $this->setFail($model->getError());
        }else{
            $oneData = $model->{$fun}($data['id']);
            //返回修改的值
            $this->setSuccess($oneData);
        }
    }
    /******************添加******************/
    /**
     * 插入数据库
     * @param $data
     * @param $model
     * @param string $fun
     */
    public function createAdd($data=[],$model=CONTROLLER_NAME,$fun='getOneData'){
        if(is_string($model)){
            $model = D($model);
        }
        if(empty($data)){
            $data = I('post.');
        }
        $r = $model->createAdd($data);
        if($r === FALSE){
            $this->setFail($model->getError());
        }else{
            $oneData = $model->{$fun}($r);
            //返回修改的值
            $this->setSuccess($oneData);
        }
    }
    /**********************删除***********************/
    /**
     * 删除一条数据
     * @param $map
     * @param $model
     */
    public function deleteData($map,$model=CONTROLLER_NAME){
        if(is_string($model)){
            $model = D($model);
        }
        if(!isset($map[$model->getPk()])){
            $map[$model->getPk()] = I('get.'.$model->getPk());
        }
        if(empty($map[$model->getPk()])){
            $this->setFail('缺少'.$model->getPk());
        }
        //删除
        $r = $model->deleteOneData($map);
        if($r === FALSE){
            $this->setFail($model->_error);
        }else{
            $this->setSuccess();
        }
    }
    /*************************查询****************************/
    /**
     * 方法使用的 用来以分页形式返回数据列表
     * @param array $map
     * @param string $order
     * @param mixed|string $model
     */
    public function responsePage($map=[], $order = 'id desc',$model=CONTROLLER_NAME){
        if(is_string($model)){
            $model = D($model);
        }
        $count = $model->getListForCount($map);
        $per_page = C('HOME_PER_PAGE_NUM', null, false);
        if($per_page === false){
            $page = new \Gy_Library\GyPage($count);
        }
        else{
            $page = new \Gy_Library\GyPage($count, $per_page);
        }
        $data_list = $model->getListForPageData($map, $page->nowPage, $page->listRows, $order);
        $this->setSuccessMessage($data_list,$count,$per_page);
    }

    /**
     * 表名或者是模型的实例
     * @param $id
     * @param $table
     * @param string $word
     */
    public function responseOneData($id,$table=CONTROLLER_NAME, $word='未找到相关的信息'){
        if(is_string($table)){
            $data = D($table)->getOneData($id);
        }else{
            $data = $table->getOneData($id);
        }
        if(empty($data)){
            $this->setFail($word);
        }else{
            $this->setSuccess($data);
        }
    }

    /**
     * @param array $map
     * @param string $order
     * @param $model 表名或者是模型的实例
     */
    public function responseList($map=[], $order = 'id desc',$model=CONTROLLER_NAME){
        if(is_string($model)){
            $model = D($model);
        }
        $data_list = $model->where($map)->order($order)->select();
        $this->setSuccessMessage($data_list);
    }

}
